<template>
	<view class="d-comment-frame">
		<view>
			<uni-easyinput ref="uipt" @confirm="posComment" suffixIcon="paperplane" v-model="commentContent"
				:placeholder="placeholder" @iconClick="posComment" :styles="{borderColor: '#0199fe'}">
			</uni-easyinput>
		</view>
	</view>
</template>

<script>
	import {
		getLocation,
	} from "@/utils/common.js"
	import {
		store
	} from '@/uni_modules/uni-id-pages/common/store.js'


	const db = uniCloud.database()

	export default {
		name: "comment-frame",
		props: {
			commentObj: {
				type: Object,
				default: () => {
					return {

					}
				}
			},
			placeholder: {
				type: String,
				default: "开始评论8~~~"
			}
		},
		created() {
			// console.log();
		},
		data() {
			return {
				commentContent: '',
			};
		},
		methods: {
			async posComment() {
				try {
					if (!store.hasLogin) {
						uni.showToast({
							"title": "请登录~~",
							icon: "none",
						})
						return
					}
					if (!this.commentContent) {
						uni.showToast({
							title: "评论不能为空",
							icon: "none"
						})
						return;
					}

					let {
						province,
						city
					} = await getLocation();
					const addData = {
						"comment_content": this.commentContent,
						province,
						city,
						...this.commentObj
					}
					db.collection("comment").add(addData).then(res => {
						uni.showToast({
							title: "评论成功"
						})
						console.log(res);
						if (this.commentObj.comment_type == 0) this.selfIncreaLikeCount()

						const userInfo = uni.getStorageSync('uni-id-pages-userInfo')
						const user_id = [{
							avatar_file: userInfo.avatar_file,
							username: userInfo.username,
						}]
						// console.log({
						// 	userInfo,
						// 	user_id
						// });
						// debugger
						this.$emit("emitComment", {
							name: 'dzl',
							comment_date: Date.now(),
							...addData, // 这里有user_id字段，所以需要放到上面
							user_id,
						})
						this.commentContent = ''
					})
				} catch (e) {
					console.log('评论失败了', e);
					uni.showToast({
						title: "评论失败",
						icon: "none"
					})
				}
			},
			async selfDecrementLikeCount() {
				const result = await uniCloud.importObject('articleOpera', {
					customUI: true
				}).selfIncreas(this.commentObj._id, 'comment_count', -1)
			},
			async selfIncreaLikeCount() {
				console.log('自增');
				const result = await uniCloud.importObject('articleOpera', {
					customUI: true
				}).selfIncreas(this.commentObj.article_id, 'comment_count')
			},
		}
	}
</script>

<style lang="scss" scoped>
	.d-comment-frame {
		width: 100%;
		height: 120rpx;
		background: #fff;
		padding: 20rpx 30rpx;
		box-sizing: border-box;
		position: fixed;
		bottom: 0;
		left: 0;
		z-index: 100;
	}
</style>